|
1
|
|
|
import {ForeignKey} from "../Model"; |
|
2
|
|
|
import {IndexInterface, ModelInterface} from "../../JeloquentInterfaces"; |
|
3
|
|
|
|
|
4
|
|
|
export default class Index implements IndexInterface { |
|
5
|
|
|
|
|
6
|
|
|
private _indexes: Map<string, Map<string|number, Set<string|number>>>; |
|
7
|
|
|
|
|
8
|
|
|
private indexedFields: Set<string>; |
|
9
|
|
|
|
|
10
|
|
|
private splitIndexNames: Map<string, Array<string>>; |
|
11
|
|
|
|
|
12
|
|
|
constructor() { |
|
13
|
|
|
this._indexes = new Map(); |
|
14
|
|
|
this.indexedFields = new Set(); |
|
15
|
|
|
this.splitIndexNames = new Map(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
get indexes(): Map<string, Map<string|number, Set<string|number>>> { |
|
19
|
|
|
return this._indexes; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
static add(model: ModelInterface, foreignKeyField: ForeignKey): void { |
|
23
|
|
|
globalThis.Store.database().addIndex(model.className, foreignKeyField.foreignKey, foreignKeyField.value, model.primaryKey); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @deprecated |
|
28
|
|
|
*/ |
|
29
|
|
|
static addIndex(model: ModelInterface, foreignKeyField:ForeignKey): void { |
|
30
|
|
|
this.add(model, foreignKeyField); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
static register(model: ModelInterface, indexName: string): void { |
|
34
|
|
|
globalThis.Store.database().registerIndex(model.className, indexName); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @deprecated |
|
39
|
|
|
*/ |
|
40
|
|
|
static registerIndex(model: ModelInterface, indexName: string): void { |
|
41
|
|
|
this.register(model, indexName) |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
static remove(model: ModelInterface, foreignKeyField: ForeignKey): void { |
|
45
|
|
|
globalThis.Store.database().removeIndex(model.className, foreignKeyField.foreignKey, foreignKeyField.previousValue, model.primaryKey); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @deprecated |
|
50
|
|
|
*/ |
|
51
|
|
|
static removeIndex(model: ModelInterface, foreignKeyField: ForeignKey): void { |
|
52
|
|
|
this.remove(model, foreignKeyField); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
static removeTmpIdFromIndex(model: ModelInterface) { |
|
56
|
|
|
const className = model.className; |
|
57
|
|
|
model.dirtyFields.filter(field => field instanceof ForeignKey).forEach((field) => { |
|
58
|
|
|
globalThis.Store.database().removeIndex(className, field.name, field.originalValue, model._tmpId); |
|
59
|
|
|
}); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public addValue(indexName: string, lookUpKey:string|number, id:string|number): void { |
|
63
|
|
|
if (!this._indexes.has(indexName) || id === null) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
const index = this.index(indexName); |
|
68
|
|
|
if (!(index.has(lookUpKey))) { |
|
69
|
|
|
this.registerLookUpKey(indexName, lookUpKey, id); |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
const keys = this.indexLookUpKey(indexName, lookUpKey); |
|
73
|
|
|
if (keys.has(id)) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
keys.add(id); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public addValueByModel(model: ModelInterface) { |
|
80
|
|
|
for (const [indexName] of this._indexes) { |
|
81
|
|
|
this.addValue( |
|
82
|
|
|
indexName, |
|
83
|
|
|
this.getLookUpValue(model, indexName), |
|
84
|
|
|
model.primaryKey |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public getIndexByKey(key: string): Map<string|number, Set<string|number>> { |
|
90
|
|
|
return this.index(key); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public getLookUpValue(model: ModelInterface, fieldName: string): string|number|null { |
|
94
|
|
|
const lookUpValue = this.splitIndexNames.get(fieldName); |
|
95
|
|
|
let returnValue = null; |
|
96
|
|
|
let indexLookUpValue = model; |
|
97
|
|
|
for (const lookUpField of lookUpValue) { |
|
98
|
|
|
if (indexLookUpValue[`original_${lookUpField}`] === null) { |
|
99
|
|
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
indexLookUpValue = indexLookUpValue[`original_${lookUpField}`]; |
|
102
|
|
|
returnValue = indexLookUpValue; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return returnValue ?? null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public register(indexName: string): void { |
|
109
|
|
|
if (!this._indexes.has(indexName)) { |
|
110
|
|
|
this.indexedFields.add(indexName); |
|
111
|
|
|
this.splitIndexNames.set(indexName, indexName.split('.')); |
|
112
|
|
|
this._indexes.set(indexName, new Map()); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @deprecated |
|
118
|
|
|
*/ |
|
119
|
|
|
public registerIndex(indexName: string): void { |
|
120
|
|
|
this.register(indexName); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public registerLookUpKey(indexName, lookUpKey, id) { |
|
124
|
|
|
this.index(indexName).set(lookUpKey, new Set([id])); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public removeValue(indexName: string, lookUpKey:string|number, id:string|number): void { |
|
128
|
|
|
this.indexLookUpKey(indexName, lookUpKey).delete(id); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public removeValueByModel(model: ModelInterface): void { |
|
132
|
|
|
for (const [indexName] of this._indexes) { |
|
133
|
|
|
this.removeValue( |
|
134
|
|
|
indexName, |
|
135
|
|
|
this.getLookUpValue(model, indexName), |
|
136
|
|
|
model.primaryKey |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public truncate(): void { |
|
142
|
|
|
for (const key in this._indexes) { |
|
143
|
|
|
this.index(key).clear(); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public unregisterLookUpKey(indexName:string, lookUpKey:string|number): void { |
|
148
|
|
|
this.index(indexName).delete(lookUpKey); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
private index(index: string): Map<string|number, Set<string|number>> { |
|
152
|
|
|
return this._indexes.get(index); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
private indexLookUpKey(index: string, key: string|number): Set<string|number> { |
|
156
|
|
|
return this.index(index).get(key); |
|
157
|
|
|
} |
|
158
|
|
|
} |